var sqlite = require('sqlite3').verbose();
var fs = require('fs');
const path = require('path');
let path1 = path.join(__dirname, '../paxy.db3')
var db = new sqlite.Database(path1);
console.log("333")
class Sqlite {
    constructor() {
        this.instance
        this.db = null
    }
    run(sql, params) {
        return new Promise((resolve, reject) => {
            db.run(sql, params, (err) => {
                if (err === null) {
                    resolve(err)
                } else {
                    reject(err)
                }
            })
        })
    }
    // 运行多条sql
    exec(sql) {
        return new Promise((resolve, reject) => {
            console.log(sql)
            db.exec(sql, (err) => {
                if (err === null) {
                    resolve("200")
                } else {
                    reject(err)
                }
            })
        })
    }

    // 查询单条
    find(sql, params) {
        return new Promise((resolve, reject) => {
            db.get(sql, params, (err, data) => {
                if (err) {
                    console.log(err, 'err')
                    reject('err')
                } else {
                    console.log(data, "data")
                    resolve(data)
                }
            })
        })
    }
    //查询所有
    findAll(sql, params) {
        return new Promise((resolve, reject) => {
            db.all(sql, params, (err, data) => {
                if (err) {
                    reject("失败")
                } else {
                    resolve(data)
                }
            })
        })
    }
    // 查询一条数据
    get(sql, params) {
        return new Promise((resolve, reject) => {
            db.get(sql, params, (err, data) => {
                if (err) {
                    console.log(err, 'err')
                    reject(err)
                } else {
                    console.log(data, "data")
                    resolve("200")
                }
            })
        })
    }

    // 新增一项
    addOne(sql, params) {
        return new Promise((resolve, reject) => {
            db.get(sql, params, (err, data) => {
                if (err) {
                    console.log(err, 'err')
                    reject(err)
                } else {
                    console.log(data, "data")
                    resolve("200")
                }
            })
        })
    }
    // 查询所有数据
    all(sql, params) {
        return new Promise((resolve, reject) => {
            db.all(sql, params, (err, data) => {

                if (err) {

                    reject(err)
                } else {

                    resolve(data)
                }
            })
        })
    }
    // 关闭数据库
    close() {
        db.close()
    }

    // 单例
    static getInstance() {
        this.instance = this.instance ? this.instance : new Sqlite()
        return this.instance
    }
}

// export default Sqlite
exports.Sqlite = Sqlite;